const app = getApp()
const HOST = require('../../../../config.js').host

Page({
    auth (e) {
        let self = this

        wx.showLoading({
            title: '正在验证...'
        })

        wx.request({
            url: HOST,
            method: 'GET',
            success (res) {
                wx.hideLoading()
                if (res.data.sysCode === 1) {
                    const rst = res.data.data

                    if (rst) {
                        app.globalData.isGuide = true
                        self.navigate('ticket')
                    } else {
                        app.globalData.isGuide = false
                        self.navigate('home')
                    }
                }
            },
            complete () {
                wx.hideLoading()
            }
        })
    },

    verify () {
        let self = this
        wx.request({
            url: HOST,
            method: 'GET',
            success (res) {
                if (res.data.sysCode === 1) {
                    const rst = res.data.data

                    if (rst) { // 是导游
                        self.navigate('ticket')
                    } else if (rst === '1') { // 未知
                        self.navigate('local')
                    } else {
                        self.navigate('home')
                    }
                }
            }
        })
    },

    navigate (direction) {
        if (direction === 'local') { // 身份未知
            wx.showModal({
                title: '请先验证身份',
                content: '请输入您的姓名和手机号码以验证您是否是导游',
                showCancel: false                
            })
        } else if (direction === 'ticket') { // 是导游
            wx.redirectTo({
                url: '../ticket?id=1',
            })
        } else { // 不是导游
            wx.showModal({
                title: '抱歉',
                content: '非常抱歉，此功能暂时只支持导游使用',
                showCancel: false,
                success (res) {
                    if (res.confirm) {
                        wx.switchTab({
                            url: '../../../home/home',
                        })
                    }
                }
            })
        }
    },

    onLoad () {
        let isGuide = app.globalData.isGuide
        if (typeof isGuide !== 'boolean') {
            this.verify()
        } else if (isGuide) {
            this.navigate('ticket')
        } else {
            this.navigate('home')
        }
    }
})